package com.example.batterynotification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.RemoteViews;

import com.example.batterynotification.MainActivity;
import com.example.batterynotification.R;

public class ManageNotifications {
	private static NotificationManager notificationManager;
	static final int MAIN_NOTIFICATION_ID = 1;
	static final int HIGH_NOTIFICATION_ID = 2;
	static final int LOW_NOTIFICATION_ID = 3;
	private static Context context;
	private static Notification notification;
	private static Intent intent;
	private static PendingIntent pIntent;

	public ManageNotifications(AppContext c) {
		context = c.getAppContext();
		new Preferences(c);
	}

	public static void createNotification(String Timer) {
		removeNotification();
		intent = new Intent(context, MainActivity.class);
		pIntent = PendingIntent.getActivity(context, 0, intent, 0);

		notification = new Notification.Builder(context)
				.setContentText("Battery Full").setSmallIcon(R.drawable.icon)
				.setContentIntent(pIntent).setOngoing(true).build();

		RemoteViews notificationView = createRomteView(Timer);
		notification.contentView = notificationView;

		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(MAIN_NOTIFICATION_ID, notification);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);		
	}

	private static RemoteViews createRomteView(String Timer) {
		RemoteViews notificationView = new RemoteViews(
				context.getPackageName(), R.layout.main_notification_layout);

		// fill the main item (current level).............................
		if (BatteryReceiverForegroundResources.getBatteryIntent() != null)
			notificationView.setTextViewText(R.id.current_level,
					BatteryReceiverForegroundResources.getBatteryIntent()
							.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) + " %");
		else
			notificationView.setTextViewText(R.id.current_level,
					+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0) + " %");
		// fill the second item...........
		if (BatteryReceiverForegroundResources.getBatteryIntent() != null)		
		if (BatteryReceiverForegroundResources.getBatteryIntent().getIntExtra(BatteryManager.EXTRA_STATUS, 0)==BatteryManager.BATTERY_STATUS_CHARGING) {
			notificationView.setTextViewText(R.id.item1Title, "Time to reach "
					+ Preferences.getIntPreferences("prefHighBatteryLevel")
					+ "%");
			notificationView.setTextViewText(R.id.item1Value, Timer);

		} 
		else if(BatteryReceiverForegroundResources.getBatteryIntent().getIntExtra(BatteryManager.EXTRA_STATUS, 0)==BatteryManager.BATTERY_STATUS_DISCHARGING) {
			notificationView.setTextViewText(R.id.item1Title, "Time to reach "
					+ Preferences.getIntPreferences("prefLowBatteryLevel")
					+ "%");
			notificationView.setTextViewText(R.id.item1Value, Timer);
		}
		return notificationView;
	}

	public static void updateMainNotification(CharSequence currentLevel,
			String TimeRemaining, CharSequence notificationLevel) {
		removeNotification();
		intent = new Intent(context, MainActivity.class);
		pIntent = PendingIntent.getActivity(context, 0, intent, 0);
		notification = new Notification.Builder(context)
				.setContentText("Battery Full").setSmallIcon(R.drawable.icon)
				.setContentIntent(pIntent).setOngoing(true).build();

		RemoteViews notificationView = updateRemoteView(currentLevel,
				TimeRemaining, notificationLevel);
		notification.contentView = notificationView;

		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(MAIN_NOTIFICATION_ID, notification);
		
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
	}

	private static RemoteViews updateRemoteView(CharSequence currentLevel,
			CharSequence TimeRemaining, CharSequence notificationLevel) {
		RemoteViews notificationView = new RemoteViews(
				context.getPackageName(), R.layout.main_notification_layout);

		if (currentLevel != null)
			notificationView.setTextViewText(R.id.current_level, currentLevel
					+ " %");
		
		if (TimeRemaining != null) 
			notificationView.setTextViewText(R.id.item1Value, TimeRemaining);
			
		if (notificationLevel != null)	
			notificationView.setTextViewText(R.id.item1Title, 
					notificationLevel
					);
			

		return notificationView;
	}

	public static void removeNotification() {
		try {
			if (notificationManager == null)
				notificationManager = (NotificationManager) context
						.getSystemService(Context.NOTIFICATION_SERVICE);

			notificationManager.cancel(MAIN_NOTIFICATION_ID);
		} catch (Exception ex) {

		}
	}

	public static void batteryFullNotification() {
		//removeNotification();
		intent = new Intent(context, MainActivity.class);
		pIntent = PendingIntent.getActivity(context, 0, intent, 0);
		// intent = new Intent(context, notificationSoundService.class);
		// pIntent=PendingIntent.getService (context, 25, intent, 0);
		long[] pattern = null;
		if (Preferences.getBoolPreferences("prefHighVibrate")) {
			pattern = new long[] { 0, 100, 1000 };

		} else {
			pattern = new long[] { 0 };
		}
		// delete intent
		Intent notificationDeleteIntent = new Intent(context,
				NotificationBroadcastReceiver.class);
		PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context,
				0, notificationDeleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);

		notification = new Notification.Builder(context)
				.setContentText("Battery Full").setVibrate(pattern)
				.setSmallIcon(R.drawable.full).setContentIntent(pIntent)
				.setDeleteIntent(deletePendingIntent).build();

		// ++ Mohamed abdelhamid: start the sound thread
		if (!Preferences.getBoolPreferences("prefHighMute")) {
			String strRingtonePreference = Preferences
					.getStringPreferences("HighRingTone");
			Intent notificationSoundManagerIntent = new Intent(context,
					notificationSoundService.class);
			notificationSoundManagerIntent.putExtra("sound",
					strRingtonePreference);
			Log.i("NS", "the notificationService sound started");
			try{
			context.startService(notificationSoundManagerIntent);
			
	
			// start the thread ..............
			String time = Preferences.getStringPreferences("RingToneTimePref");
			int waitTime = 0;
			if (!time.equals("never"))
			{
				if (time.equals("30"))
					waitTime = 30;
				else if (time.equals("1"))
					waitTime = 60;
				else if (time.equals("2"))
					waitTime = 60;

				
				final long finalWaitTime = waitTime*1000;
				Log.i("NS", "Waittime = " + finalWaitTime);
			
				new Thread(new Runnable() {
					public void run() {

						try {
							 synchronized(this){
							wait(finalWaitTime);
							 }
						} catch (InterruptedException e) {
							Log.i("NS", "catch :( " + e.toString());
						}
						Log.i("NS", "after wait");
						try{
						Intent serviceIntent= new Intent(context,notificationSoundService.class);
						context.stopService(serviceIntent);
						}catch(Exception ex)
						{
							
						}

					}
				}).start();

			}
			}
			catch(Exception ex)
			{
				Log.i("NS","Exception= " +ex.toString() );
			}			
		}
		// -- Mohamed abdelhamid: start the sound thread

		// hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(HIGH_NOTIFICATION_ID, notification);

	}

	public static void batteryLowNotification() {
		Log.i("LC", "batteryLowNotification()");
		intent = new Intent(context, MainActivity.class);
		pIntent = PendingIntent.getActivity(context, 0, intent, 0);
		long[] pattern = null;
		if (Preferences.getBoolPreferences("prefLowVibrate")) {
			pattern = new long[] { 0, 100, 1000 };

		} else {
			pattern = new long[] { 0 };
		}
		notification = new Notification.Builder(context)
				.setContentText("Battery Low").setVibrate(pattern)
				.setSmallIcon(R.drawable.full).setContentIntent(pIntent)
				.build();
		// ++ Mohamed abdelhamid: add the sound to the notification directly
		if (!Preferences.getBoolPreferences("prefLowMute")) {
			String strRingtonePreference = Preferences
					.getStringPreferences("LowRingTone");
			notification.sound = Uri.parse(strRingtonePreference);
		}

		// -- Mohamed abdelhamid: add the sound to the notification directly
		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		// hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		notificationManager.notify(LOW_NOTIFICATION_ID, notification);
	}
}
